#include "OctTree.h"

OctTree::OctTree(Vector3<float> origin, float width, float height, float depth):
	_MainNode(new QuadTreeNode(NULL, BoundingBox(origin,width,height,depth), this))
{
}


OctTree::~OctTree(void)
{
}

bool OctTree::addGameObject(Colider* obj)
{
	OctChild* add = new OctChild();
	add->_gameObject = obj;
	add->_location = _MainNode->AddObject(add);
	if(add->_location != NULL)
	{
		_children.push_back(add);
		add->_location->Orginize();
		return true;
	}
	else
	{
		return false;
	}
}

void OctTree::removeGameObject(Colider* obj)
{
	std::vector<OctChild*>::iterator itor; 
	for(itor = _children.begin(); itor != _children.end(); itor++ )
	{
		if((*itor)->_gameObject == obj)
		{
			(*itor)->_location->RemoveObject((*itor));
			_children.erase(itor);
			return;
		}
	}
}

void OctTree::Draw()
{
	_MainNode->Draw();

	/*for(int i = 0; i < _children.size(); i++)
	{
		_children[i]->_location->Draw();
	}*/
}


bool OctTree::ObjectMoved(Colider* obj)
{
	for(int i = 0; i < _children.size(); i++)
	{
		if(_children[i]->_gameObject == obj)
		{
			_children[i]->_location->Orginize();
			return true;
		}
	}
	return false;
}

std::vector<Colider*> OctTree::InBox(Colider* obj)
{
	for(int i = 0; i < _children.size(); i++)
	{
		if(_children[i]->_gameObject == obj)
		{
			return _children[i]->_location->InBox();
		}
	}
}

